package com.keyinong.caizhengting.myfragement;

/**
 * 团队取餐二维码
 */

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.keyinong.caizhengting.R;
import com.keyinong.caizhengting.tools.HttpTools;
import com.keyinong.caizhengting.tools.ImageUtils;
import com.loopj.android.http.JsonHttpResponseHandler;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class TeamerweimaActivity extends Activity {

    HttpTools httptools = null;
    /**
     * 二维码
     */
    ImageView iv_erweima;
    /**
     * 确定
     */
    Button btn_queding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_teamerweima);
        iv_erweima = (ImageView) findViewById(R.id.iv_erweima2);
        btn_queding = (Button) findViewById(R.id.btn_queding2);
        btn_queding.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        Erweima();
    }

    private void Erweima() {
        SharedPreferences sp = getSharedPreferences("spReg",
                Context.MODE_PRIVATE);
        String token = sp.getString("data", "");
        SharedPreferences sp2 = getSharedPreferences("teamOrderid",
                Context.MODE_PRIVATE);
        String id = sp2.getString("teamOrderid", "");
        Log.i("zq", "token--" + token);
        Log.i("zq", "teamOrderid--" + id);
        if (httptools == null) {
            httptools = new HttpTools(getApplicationContext());
        }
        httptools.orderdetail(token, id, new JsonErweima());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.erweima, menu);
        return true;
    }

    public class JsonErweima extends JsonHttpResponseHandler {
        @Override
        public void onSuccess(int statusCode, Header[] headers,
                              JSONObject response) {
            // TODO Auto-generated method stub
            super.onSuccess(statusCode, headers, response);
            try {
                Log.i("zq", response.toString());
                boolean code = response.getBoolean("code");
                String msg = response.getString("msg");
                if (code == true) {
                    JSONObject object = response.getJSONObject("data");
                    String pic = object.getString("pic");
                    Log.i("zq", "pic---" + pic);
                    ImageUtils.displayImage(getApplicationContext(), pic, iv_erweima, R.drawable.zhangzaijiazai1);
                } else {
                    Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

}
